iT邦幫忙

2024 iThome 鐵人賽

0
Kubernetes

都什麼年代了,還在學 Kubernetes系列 第 33

學 Kubernetes 的第三十三天 - Autoscaling - 實作 HPA

  • 分享至 

  • xImage
  •  

在開始實作之前,讓我們稍微複習 HPA 是什麼:

HorizontalPodAutoscaler(簡稱 HPA ) 自動更新工作負載資源(例如 Deployment 或者 StatefulSet),目的是自動擴縮工作負載以滿足需求。HPA 的重點是調整 Pod 的數量以適應實際負載。

開始之前

叢集必需部署 Metrics Server 。因為 HPA 需要透過 kubelet 收集 Metrics Server。部署安裝的方式可以回頭去看之前的章節。

運行 nginx 並暴露服務

組態檔案: hpa-example.yaml

apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx
spec:
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx
        ports:
        - containerPort: 80
        resources:
          limits:
            cpu: 250m
          requests:
            cpu: 100m
---
apiVersion: v1
kind: Service
metadata:
  name: nginx-svc
  labels:
    run: nginx-svc
spec:
  ports:
  - port: 80
  selector:
    app: nginx
  • 部署資源
kubectl apply -f hpa-example.yaml
  • 檢查資源狀態
kubectl get deployment,pod,service

輸出如下

NAME                    READY   UP-TO-DATE   AVAILABLE   AGE
deployment.apps/nginx   1/1     1            1           119s

NAME                        READY   STATUS    RESTARTS   AGE
pod/nginx-769cfd889-wkgm5   1/1     Running   0          102s

NAME                 TYPE        CLUSTER-IP     EXTERNAL-IP   PORT(S)   AGE
[...]
service/nginx-svc    ClusterIP   10.96.250.1    <none>        80/TCP    119s

建立 HorizontalPodAutoscaler

  • 建立 HPA
kubectl autoscale deployment nginx --cpu-percent=50 --min=1 --max=10

或使用以下組態檔案

apiVersion: autoscaling/v2
kind: HorizontalPodAutoscaler
metadata:
  name: nginx
  namespace: default
spec:
  maxReplicas: 10
  metrics:
  - resource:
      name: cpu
      target:
        averageUtilization: 50
        type: Utilization
    type: Resource
  minReplicas: 1
  scaleTargetRef:
    apiVersion: apps/v1
    kind: Deployment
    name: nginx

HPA 控製器將增加和減少副本的數量 (通過更新 Deployment)以保持所有 Pod 的平均 CPU 利用率為 50%。 Deployment 然後更新 ReplicaSet —— 這是所有 Deployment 在 Kubernetes 中工作方式的一部分 —— 然後 ReplicaSet 根據其 .spec 的更改新增或刪除 Pod。

  • 查看 HPA 狀態
kubectl get hpa
---
NAME    REFERENCE          TARGETS       MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   cpu: 0%/50%   1         10        1          27s

CPU 利用率是 0%,是因為我們尚未傳送任何請求到 nginx 伺服器。

增加負載

接下來,看看自動擴縮器如何對增加的負載做出反應。

  • 打開一個新終端 t1,啟動一個 Pod 作為客戶端,並向 nginx service 不斷傳送查詢請求
kubectl run -it load-generator --rm --image=busybox:1.36 --restart=Never -- /bin/sh -c "while sleep 0.01; do wget -q -O - nginx; done"

完成後,監控以下資源:

  • 打開一個新終端 t2 ,監控 HPA 的變化
kubectl get hpa nginx --watch
  • 打開一個新終端 t3 ,監控 Deployment nginx 事件
kubectl get events --watch --field-selector involvedObject.kind=Deployment,involvedObject.name=nginx 

等待幾分鐘,查看結果:

  • 回到終端 t2,可以看到 CPU 負載升高,然後又因為副本增加而又下降
NAME    REFERENCE          TARGETS        MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   cpu: 0%/50%    1         10        1          29m
nginx   Deployment/nginx   cpu: 10%/50%   1         10        1          29m
nginx   Deployment/nginx   cpu: 90%/50%   1         10        1          29m
nginx   Deployment/nginx   cpu: 90%/50%   1         10        2          30m
nginx   Deployment/nginx   cpu: 50%/50%   1         10        2          30m
nginx   Deployment/nginx   cpu: 52%/50%   1         10        2          31m
nginx   Deployment/nginx   cpu: 55%/50%   1         10        2          32m
nginx   Deployment/nginx   cpu: 57%/50%   1         10        3          32m
nginx   Deployment/nginx   cpu: 45%/50%   1         10        3          32m
nginx   Deployment/nginx   cpu: 37%/50%   1         10        3          32m
nginx   Deployment/nginx   cpu: 38%/50%   1         10        3          33m
nginx   Deployment/nginx   cpu: 36%/50%   1         10        3          33m
  • 回到終端 t3,可以看到 Deployment 觸發了 Scale up 事件
LAST SEEN   TYPE     REASON              OBJECT             MESSAGE
0s          Normal   ScalingReplicaSet   deployment/nginx   Scaled up replica set nginx-7fcc74d4cf to 2 from 1
0s          Normal   ScalingReplicaSet   deployment/nginx   (combined from similar events): Scaled up replica set nginx-7fcc74d4cf to 3 from 2

查詢 Deployment

kubectl get deployments nginx
---
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   3/3     3            3           42m

可以看到,Deployment 與 HPA 對應的副本數量相同。HPA 已經為增加的負載做出資源的調整。

減少負載

現在,讓我們關閉剛剛的 Pod 客戶端,看看 HPA 會如何反應。

  • 回到終端 t1,輸入 <Ctrl> + C 來終止負載的產生。

等待幾分鐘,查看結果:

  • 回到終端 t2,可以看到 CPU 負載升高,然後又因為副本增加而又下降
NAME    REFERENCE          TARGETS              MINPODS   MAXPODS   REPLICAS   AGE
nginx   Deployment/nginx   cpu: 36%/50%         1         10        3          3m45s
nginx   Deployment/nginx   cpu: 28%/50%         1         10        3          4m16s
nginx   Deployment/nginx   cpu: 1%/50%          1         10        3          4m31s
nginx   Deployment/nginx   cpu: 0%/50%          1         10        3          5m1s
nginx   Deployment/nginx   cpu: 0%/50%          1         10        3          9m1s
nginx   Deployment/nginx   cpu: 0%/50%          1         10        2          9m16s
nginx   Deployment/nginx   cpu: 0%/50%          1         10        1          9m31s
nginx   Deployment/nginx   cpu: 0%/50%          1         10        1          9m46s
  • 回到終端 t3,可以看到 Deployment 觸發了 Scale up 事件
LAST SEEN   TYPE     REASON              OBJECT             MESSAGE
0s          Normal   ScalingReplicaSet   deployment/nginx   Scaled down replica set nginx-7fcc74d4cf to 2 from 3
0s          Normal   ScalingReplicaSet   deployment/nginx   Scaled down replica set nginx-7fcc74d4cf to 1 from 2
60m         Normal   ScalingReplicaSet   deployment/nginx   Scaled up replica set nginx-769cfd889 to 1
  • 查詢 Deployment
kubectl get deployments nginx
---
NAME    READY   UP-TO-DATE   AVAILABLE   AGE
nginx   1/1     1            1           23m

可以看到,Deployment 與 HPA 對應的副本數量相同。


上一篇
學 Kubernetes 的第三十二天 - Autoscaling - 概論
下一篇
學 Kubernetes 的第三十四天 - Autoscaling - 實作 VPA
系列文
都什麼年代了,還在學 Kubernetes37
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言